<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form
          ref="userForm"
          label-width="220px"
          :model="form"
          :rules="rules"
        >
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input size="mini" class="inputW" v-model="form.username" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input
                  size="mini"
                  class="inputW"
                  v-model="form.workNumber"
                  disabled
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <!-- 编辑时，手机号不能改，因为他是用户的登录账号，禁用；新增时需要用户输入，不能禁用-->
                <el-input
                  size="mini"
                  class="inputW"
                  v-model="form.mobile"
                  maxlength="11"
                  :disabled="!!$route.params.id"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item
                label="部门"
                prop="departmentId"
                v-model="form.departmentId"
              >
                <!-- 放置及联部门组件 -->
                <SelectTree v-model="form.departmentId"></SelectTree>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select
                  size="mini"
                  class="inputW"
                  v-model="form.formOfEmployment"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.label"
                    :value="item.id"
                  >
                  </el-option
                ></el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                  v-model="form.timeOfEntry"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  size="mini"
                  type="date"
                  class="inputW"
                  v-model="form.correctionTime"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <imageUpload v-model="form.staffPhoto"></imageUpload>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left: 220px">
              <el-button size="mini" type="primary" @click="save"
                >保存更新</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import SelectTree from "./components/selectTree.vue";
import { addEmployee, getEmployeeDetail, updataEmployee } from "@/api/employee";
import imageUpload from "./components/image-upload.vue";
export default {
  name: "EmployeeDetail",
  components: { SelectTree, imageUpload },
  data() {
    let validatorData = (rule, value, callback) => {
      // +号的作用： 1. 两个操作数都是数字的话 加和
      // 2. 只有一个操作数时强制转数字 如+'5'
      // 3. 两个操作数中有一个字符串 是字符串拼接,把另一个非字符串的转成字符串，然后拼接起来
      +new Date(value) > +new Date(this.form.timeOfEntry)
        ? callback()
        : callback(new Error("转正时间必须大于入职时间"));
    };
    return {
      form: {},
      options: [
        { id: 1, label: "正式" },
        { id: 2, label: "非正式" },
      ],
      rules: {
        username: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          {
            min: 2,
            max: 6,
            message: "姓名为2-6位",
          },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            //   pattern 正则表达式
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        formOfEmployment: [
          { required: true, message: "请选择聘用形式", trigger: "change" },
        ],
        departmentId: [
          { required: true, message: "请选择部门", trigger: "change" },
        ],
        timeOfEntry: [
          { required: true, message: "请选择入职时间", trigger: "blur" },
        ],
        correctionTime: [
          { required: true, message: "请选择转正时间", trigger: "blur" },
          {
            validator: validatorData,
          },
        ],
      },
    };
  },
  created() {
    console.log("history", history.length);
    console.log("id", this.$route.params.id);
    if (this.$route.params.id) this.getEmployeeDetail();
  },
  methods: {
    async getEmployeeDetail() {
      this.form = await getEmployeeDetail(this.$route.params.id);
      console.log("form", this.form);
    },
    async save() {
      // 1. 校验。不通过阻止
      const isOK = this.$refs.userForm.validate().catch((err) => err);
      if (!isOK) return;
      // 2. 调接口把数据传给后台
      if (this.form.id) {
        await updataEmployee(this.form);
      } else {
        await addEmployee(this.form);
      }
      // 3. 提示（成功&失败）
      this.$message.success(this.form.id ? "更新员工成功" : "新增员工成功");
      // 4. 前端更新页面
      // 5. 关闭弹窗（返回页面），重置表单
      if (history.length > 2) {
        this.$router.back(); // 返回上一个页面 因为咱们这个新增页面就来自于列表页面
      } else {
        this.$router.replace("/employee");
      }
    },
    
  },
};
</script>

<style scoped lang="scss">
.edit-form {
  background: #fff;
  padding: 20px;
  .inputW {
    width: 380px;
  }
}
</style>
